package com.liujit.bms.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.app.domain.model.BlogMemberReadRecord;
import com.liujit.bms.app.domain.params.BlogMemberReadRecordParams;
import com.liujit.bms.app.domain.query.BlogMemberReadRecordQuery;
import com.liujit.bms.app.domain.result.BlogMemberReadRecordResult;
import com.liujit.bms.app.mapper.BlogMemberReadRecordMapper;
import com.liujit.bms.app.service.BlogMemberReadRecordService;
import com.liujit.common.constants.ColumnConstant;
import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import com.liujit.common.utils.Pagination;
import com.liujit.web.utils.JwtUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description 博客会员阅读记录Service实现类
 * @Author liujun
 * @Date 2021-07-16 18:15:24
 */
@Service
public class BlogMemberReadRecordServiceImpl extends ServiceImpl<BlogMemberReadRecordMapper, BlogMemberReadRecord>
        implements BlogMemberReadRecordService {

    @Override
    public PageBean<BlogMemberReadRecordResult> getPage(BlogMemberReadRecordQuery query) {
        query.setSortName(ColumnConstant.READ_TIME);
        query.setSortOrder(SqlKeyword.DESC.getSqlSegment());
        Page<BlogMemberReadRecord> page = Pagination.page(query);
        LambdaQueryWrapper<BlogMemberReadRecord> wrapper = new LambdaQueryWrapper();
        IPage<BlogMemberReadRecord> iPage = super.page(page, wrapper);
        return PageBean.page(iPage, BlogMemberReadRecordResult.class);
    }

    @Async
    @Override
    public Boolean add(BlogMemberReadRecordParams params) {
        Long userId = JwtUtil.getUserId();
        // 添加阅读量
        super.baseMapper.addReadNum(params.getArticleId());
        BlogMemberReadRecord entity = DozerConvertor.convertor(params, BlogMemberReadRecord.class);
        // 判断当前记录是否存在
        LambdaQueryWrapper<BlogMemberReadRecord> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogMemberReadRecord::getArticleId, params.getArticleId());
        wrapper.eq(BlogMemberReadRecord::getMemberId, userId);
        BlogMemberReadRecord blogMemberReadRecord = super.getOne(wrapper);
        if (blogMemberReadRecord == null) {
            // 添加
            entity.setReadTime(new Date());
            entity.setMemberId(userId);
            return super.save(entity);
        } else {
            // 更新
            return super.updateById(new BlogMemberReadRecord() {{
                setId(blogMemberReadRecord.getId());
                setReadTime(new Date());
            }});
        }
    }

    @Override
    public Boolean del(BaseParams params) {
        BlogMemberReadRecord blogMemberReadRecord = super.getById(params.getId());
        if (blogMemberReadRecord == null) {
            throw new DataNotFoundException();
        }
        LambdaQueryWrapper<BlogMemberReadRecord> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogMemberReadRecord::getId, params.getId());
        wrapper.eq(BlogMemberReadRecord::getMemberId, JwtUtil.getUserId());
        return super.removeById(params.getId());
    }

    @Override
    public Boolean clear() {
        LambdaQueryWrapper<BlogMemberReadRecord> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogMemberReadRecord::getMemberId, JwtUtil.getUserId());
        return super.remove(wrapper);
    }
}
